define(function () {
    'use strict';

    function ctrl($scope,$state,$rootScope,LocalCacheService,FinancingService,WebService,$ionicPopup, TdxLocalFuncService){

        var par = $scope.param = new Array();

        $scope.onBackKeyDown = function(){
            $state.go("tab.index");
        };

        $scope.showIllustration = function(){
            var alertPopup = $ionicPopup.show({
                cssClass: "kDialog",
                template: "<span class='tal'>本账户数据暂只包含小财迷平台理财产品的资产数据，不含股票资产。<br>"+
                "持仓盈亏是指目前持有产品的合计总盈亏；<br>"+
                "总投资额是指目前持有产品的总金额，含产品盈亏；<br>"+
                "账户余额是指本账户内供投资人用于购买产品的资金金额，投资者可以通过充值提现功能将银行卡中的资金转账到账户余额中，也可以将可取余额提现到银行卡。<br>"+
                "账户中显示的收益数据未扣除手续费，实际收益以基金公司实际到账金额为准。<br>"+
                "如有疑问，请咨询客服：400-800-5000。</span>",
                buttons:[{
                    text: '我知道了',
                    type: 'button-default'
                }]
            });
            alertPopup.then(function(res) {
            });
        };

        $scope.changeShow = function() {
            LocalCacheService.set("AssetsIndexCtrl_show", par.show ? "1" : "0");
        };

        $scope.doRefresh = function() {
            init();
            $scope.$broadcast("scroll.refreshComplete");
        };

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        $scope.$watch("status", function(newValue, oldValue){
            if(newValue && newValue.length >= 3) {
                par.total = par.total_invest + par.enable_balance + par.market_values - par.repayment_amount;
            }
        });

        $scope.goRechargeAndWithdraw = function(){
            $state.go("recharge-and-withdraw");
            /*
            if(TdxLocalFuncService.isTDX()){
                TdxLocalFuncService.openYzzz().then(init);
            } else {
                $state.go("recharge-and-withdraw");
            }*/
        };

        function init(){

            $scope.status = "";
            par.total_invest = 0;
            par.enable_balance = 0;
            par.market_values = 0;
            par.repayment_amount = 0;
            par.total = 0;

            $scope.user = LocalCacheService.getUser();
            var showMoney = LocalCacheService.get("AssetsIndexCtrl_show");
            if(!showMoney) {
                par.show = true;
            }else {
                par.show = showMoney == '1';
            }
            par.market_values = 0;
            WebService.totalMoney().then(function(data){
                $scope.status += "1";
                par.market_values = parseFloat(data.market_value);
            });
            /*FinancingService.getIndexInfo({
                client_id: $scope.user.client_id,
                fund_account: $scope.user.fund_account
            }).then(function(data) {
                $scope.status += "1";
                if(data.error_no == 0) {
                    par.xdb = true;
                    par.repayment_amount = parseFloat(data.repayment_amount);
                } else {
                    par.xdb = false;
                }
            });*/
            //获取存管银行信息
            WebService.getUserDepositBankCards().then(
                function (data) {
                    if(data.length > 0 && "1,2".indexOf(data[0].bkaccount_regflag) > -1){
                        par.show_recharge = true;
                        //查询保证金余额
                        queryBalance();
                    }
                }
            );

            //查询收益信息
            WebService.queryTotalIncome().then(
                function (data) {
                    $scope.status += "1";
                    if(data){
                        par.total_income = parseFloat(data.total_income);
                        par.total_invest = parseFloat(data.total_invest);
                    }
                }
            );
        }

        function queryBalance(){
            WebService.queryBalance().then(
                function(data){
                    $scope.status += "1";
                    par.enable_balance = parseFloat(data.enable_balance);
                }
            );
        }
    }

    ctrl.$inject = ['$scope','$state','$rootScope','LocalCacheService','FinancingService','WebService','$ionicPopup', 'TdxLocalFuncService'];
    return ctrl;
});
